//
//  NewWalletViewController.m
//  NewPro
//
//  Created by RYX on 2019/4/10.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "NewWalletViewController.h"
#import "NewWalletCell.h"
#import "NewWalletHeaderView.h"
#import "BandViewController.h"
#import "NewWalletStore.h"
#import "NewWalletModel.h"
#import "NewWalletList.h"
#import "CodeAlertView.h"
#import "NewWalletList.h"

@interface NewWalletViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *titleArr;
@property (nonatomic,copy) NSArray *iconArr;
@property (nonatomic,copy) NSArray *bgImArr;
@property (nonatomic,strong) NewWalletHeaderView *headerView;
//@property (nonatomic,strong) CodeAlertView *codeAlertView;
@end

@implementation NewWalletViewController

- (NewWalletHeaderView *)headerView{
    
    if (!_headerView) {
        
        _headerView = [[NewWalletHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 367)];
    }
    
    return _headerView;
}
- (NSArray *)titleArr{
    
    if (!_titleArr) {
        
        _titleArr = @[@"银行卡",@"积分"];
    }
    return _titleArr;
    
}

- (NSArray *)bgImArr{
    
    if (!_bgImArr) {
        
        _bgImArr = @[@"newBg_01",@"newBg_02"];
    }
    return _bgImArr;
    
}

- (NSArray *)iconArr{
    
    if (!_iconArr) {
        
        _iconArr = @[@"newBand",@"newJifen"];
    }
    return _iconArr;
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 24.5;
        _tableView.sectionHeaderHeight = 0.5;
        _tableView.tableHeaderView = self.headerView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            [weakSelf loadData];
        }];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
        
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.equalTo(self.view);
            
        }];
        
    }
    
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self setNavBarWithTitle:@"我的钱包"];
    
    [self.tableView.mj_header beginRefreshing];

    //判断有没有设置支付密码
    [self ifHaveSetTradingPassWord];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:PUTFORWARDSUCCESS object:nil];
    
}

- (void)getTXInfo{
    
    MJWeakSelf
    NewWalletStore *store = [[NewWalletStore alloc] init];
    [store getTXInfoSuccess:^(TXModel *model) {
        
        weakSelf.headerView.txModel = model;
        
    } Failure:^(NSError *error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)loadData{
    
    MJWeakSelf
    NewWalletStore *store = [[NewWalletStore alloc] init];
    [store getAccountBalanceWithCustomId:[UserDefaultsManager sharedInstance].user_id Success:^(NewWalletModel *model) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [NewWalletList sharedInstance].selectModel = model;
        weakSelf.headerView.model = model;
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
    //提现提示信息
    [self getTXInfo];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 48;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewWalletCell *cell = [NewWalletCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = UIColor.clearColor;
    cell.bgIm.image = [UIImage imageNamed:self.bgImArr[indexPath.section]];
    cell.iconIm.image = [UIImage imageNamed:self.iconArr[indexPath.section]];
    cell.titleLa.text = self.titleArr[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
        case 0:
            
            [self goBank];
            break;
            
        default:
            break;
    }
}

- (void)goBank{
    
    BandViewController *VC = [[BandViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 判断有没有设置支付密码
- (void)ifHaveSetTradingPassWord{
    
    MJWeakSelf
    NewWalletStore *store = [[NewWalletStore alloc] init];
    [store getIfHaveSetTradingPassWordSucess:^(BOOL seted) {
        
        [NewWalletList sharedInstance].tradingPassWordSeted = seted;
        
    } Failure:^(NSError *error) {
       
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}
@end
